var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
// var logger = require('morgan');
let cors = require('cors');

var indexRouter = require('./routes/index');
let listRouter = require('./routes/list');
let courseRouter = require('./routes/course');
var usersRouter = require('./routes/users');

var app = express();

// app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use("/uploads",express.static(path.join(__dirname, 'uploads')));

app.use( cors() );

// app.use((req,res,next)=>{
//     //验证token是否为空
//     let {token=""} = req.headers;
//     if(!token){
//         res.json({code:201,data:null,msg:"token为空，请登录后访问"});
//         return;
//     }
//     next()
// })

app.use('/v1', indexRouter);
app.use('/v1/list',listRouter);
app.use('/v1/course',courseRouter);
app.use('/v1/users', usersRouter);

//404错误消息处理
app.use((req,res)=>{
    res.json({code:404,data:null,msg:"请求url与接口不匹配"})
})

module.exports = app;
